import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.border.Border;

import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.gantt.*;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class Chart extends JFrame {

	private static final long serialVersionUID = 1L;
	private static ArrayList<Integer>[] Tasks;
	private static int iloscZadan;
	private Border krawedz = BorderFactory.createLineBorder(Color.black);
	private static ArrayList<String> names;
	public Chart(String s, ArrayList<Integer>[] TasksIn, int quantity,
			String dane1, String dane2, ArrayList<String> zadania) {
		super(s);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		Chart.names=zadania;
		
		

		Tasks = TasksIn;
		iloscZadan = quantity;
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(2, 1));
		JPanel jpanel = createDemoPanel();
		jpanel.setBorder(krawedz);
		JPanel temp = new JPanel();
		JTextArea areaData1 = new JTextArea(dane1);
		areaData1.setBorder(krawedz);
		areaData1.setBackground(new Color(217, 231, 241));
		temp.add(areaData1);
		areaData1.setPreferredSize(new Dimension(400, 200));

		JTextArea areaData2 = new JTextArea(dane2);
		areaData2.setBorder(krawedz);
		areaData2.setBackground(new Color(217, 231, 241));
		temp.add(areaData2);
		areaData2.setPreferredSize(new Dimension(400, 200));

		mainPanel.add(jpanel);
		mainPanel.add(temp);

		jpanel.setPreferredSize(new Dimension(1000, 600));
		setContentPane(mainPanel);
		setPreferredSize(new Dimension(1100, 700));

		this.pack();
		RefineryUtilities.centerFrameOnScreen(this);
		this.setVisible(true);
		
	}

	private static JFreeChart createChart(IntervalXYDataset intervalxydataset) {
		JFreeChart jfreechart = ChartFactory.createXYBarChart(
				"Wykres szeregowania RMS", "Zadanie", false, "Czas",
				intervalxydataset, PlotOrientation.HORIZONTAL, true, false,
				false);
		jfreechart.setBackgroundPaint(Color.white);
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		xyplot.setRangePannable(true);
		String[] oznaczenia = new String[iloscZadan];
		for (int i = 0; i < iloscZadan; i++) {
			oznaczenia[i] = "Zad";
			System.out.println(oznaczenia[i]);
		}
		SymbolAxis symbolaxis = new SymbolAxis("Zadanie", oznaczenia);
		symbolaxis.setGridBandsVisible(false);
		xyplot.setDomainAxis(symbolaxis);
		XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
		xybarrenderer.setUseYInterval(true);
		xyplot.setRangeAxis(new DateAxis("Zadanie"));
		ChartUtilities.applyCurrentTheme(jfreechart);

		DateAxis a = new DateAxis("Czas");
		a.setDateFormatOverride(new SimpleDateFormat("S"));
		a.setRange(0, 30);
		xyplot.setRangeAxis(a);

		return jfreechart;
	}

	public static JPanel createDemoPanel() {
		JFreeChart jfreechart = createChart(createDataset());
		ChartPanel chartpanel = new ChartPanel(jfreechart);
		chartpanel.setMouseWheelEnabled(true);

		return chartpanel;
	}

	private static Date date(int milisecond) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(milisecond);
		return calendar.getTime();
	}

	private static IntervalXYDataset createDataset() {
		return new XYTaskDataset(createTasks());
	}

	private static TaskSeriesCollection createTasks() {
		TaskSeriesCollection taskseriescollection = new TaskSeriesCollection();

		for (int i = iloscZadan - 1; i >= 0; i--) {
			TaskSeries taskseries = new TaskSeries(names.get(i));
			for (int x = 0; x < Tasks[i].size(); x++) {
				taskseries.add(new Task("T" + i, date(Tasks[i].get(x)),
						date(Tasks[i].get(x) + 1)));
			}
			taskseriescollection.add(taskseries);
		}
		return taskseriescollection;
	}
}
